package com.example.schooljava.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * 因为Long类型id数据传递到前端之后失去精度导致id值发生了改变
 * 配置全局处理，Long类型转String再返回到前端
 * @author Mr peng 
 */
@Configuration
public class JackSonConfig {

	@Bean
	@Primary
	@ConditionalOnMissingBean(ObjectMapper.class)
	public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
		
		ObjectMapper objectMapper = builder.createXmlMapper(false).build();
		
		SimpleModule simpleModule = new SimpleModule();
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		
		objectMapper.registerModule(simpleModule);
		
		return objectMapper;
		
	}
	
}
